package com.moli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.constans.SystemConstants;
import com.moli.domain.ResponseResult;
import com.moli.domain.entity.Role;
import com.moli.domain.entity.RoleMenu;
import com.moli.domain.vo.PageVo;
import com.moli.mapper.RoleMapper;
import com.moli.service.RoleMenuService;
import com.moli.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： XC
 * @Date： 2023-06-08 14:17
 * @Description： 角色信息表(Role)表服务实现类
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
	@Autowired
	private RoleMenuService roleMenuService;

	@Override
	public List<String> selectRoleKeyByUserId(Long id) {
		//1.判断是否是管理员 如果是返回集合中只需要有admin
		if (id == 1L) {  //id恒等于1 L代表长整型
			List<String> roleKeys = new ArrayList<>();
			roleKeys.add("admin");
			return roleKeys;
		}
		//2.否则查询用户所具有的角色信息
		return getBaseMapper().selectRoleKeyByUserId(id);
	}

	@Override
	public ResponseResult selectRolePage(Role role, Integer pageNum, Integer pageSize) {
		LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		//目前没有根据id查询
//        lambdaQueryWrapper.eq(Objects.nonNull(role.getId()),Role::getId,role.getId());
		lambdaQueryWrapper.like(StringUtils.hasText(role.getRoleName()), Role::getRoleName, role.getRoleName());
		lambdaQueryWrapper.eq(StringUtils.hasText(role.getStatus()), Role::getStatus, role.getStatus());
		lambdaQueryWrapper.orderByAsc(Role::getRoleSort);

		Page<Role> page = new Page<>();
		page.setCurrent(pageNum);
		page.setSize(pageSize);
		page(page, lambdaQueryWrapper);

		//转换成VO
		List<Role> roles = page.getRecords();

		PageVo pageVo = new PageVo();
		pageVo.setTotal(page.getTotal());
		pageVo.setRows(roles);
		return ResponseResult.okResult(pageVo);
	}

	@Override
	@Transactional
	public void insertRole(Role role) {
		save(role);
		System.out.println(role.getId());
		if (role.getMenuIds() != null && role.getMenuIds().length > 0) {
			insertRoleMenu(role);
		}
	}

	@Override
	public void updateRole(Role role) {
		updateById(role);
		roleMenuService.deleteRoleMenuByRoleId(role.getId());
		insertRoleMenu(role);
	}

	@Override
	public List<Role> selectRoleAll() {
		return list(Wrappers.<Role>lambdaQuery().eq(Role::getStatus, SystemConstants.NORMAL));
	}

	@Override
	public List<Long> selectRoleIdByUserId(Long userId) {
		return getBaseMapper().selectRoleIdByUserId(userId);
	}

	private void insertRoleMenu(Role role) {
		List<RoleMenu> roleMenuList = Arrays.stream(role.getMenuIds())
				.map(memuId -> new RoleMenu(role.getId(), memuId))
				.collect(Collectors.toList());
		roleMenuService.saveBatch(roleMenuList);
	}
}


